package com.xjoyt.pay.master.repository;

import java.util.Date;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;

import com.xjoyt.pay.master.dao.CompanyProfitRepositoryCustom;
import com.xjoyt.pay.master.po.CompanyProfit;
import com.xjoyt.pay.repository.Repository;

public interface CompanyProfitRepository extends Repository<CompanyProfit>, CompanyProfitRepositoryCustom {

	Page<CompanyProfit> findAllByOrderIdAndCompanyId(String orderId, String companyId, Pageable pageable);

	// @Query(" select a.company.id , sum(a.money) , sum( (a.rate - a.costRate)
	// * a.money / 1000) from CompanyProfit a where a.company.id in ?1 and
	// a.addedDate >= ?2 and a.addedDate <= ?3 group by a.company.id ")
	@Query(" select a.company.id , sum(a.money) , sum( (a.rate - a.costRate) * a.money / 100) from CompanyProfit a where a.company.id in ?1 and a.addedDate >= ?2 and a.addedDate <= ?3 group by a.company.id ")
	List<Object[]> sumByCompanyIdInGroupByCompanyIdIn(List<String> companyIds, Date startDate, Date endDate);

	// @Query(" select a.company.id , sum(a.money) , sum( (a.rate - a.costRate)
	// * a.money / 1000) from CompanyProfit a where a.company.id in ?1 group by
	// a.company.id ")
	@Query(" select a.company.id , sum(a.money) , sum( (a.rate - a.costRate) * a.money / 100) from CompanyProfit a where a.company.id in ?1 group by a.company.id ")
	List<Object[]> sumByCompanyIdInGroupByCompanyIdIn(List<String> companyIds);

}
